﻿Ext.define("PSI.User.OrgEditForm", {
    createUI: function (entity) {
        var wnd = Ext.create("Ext.window.Window", {
            title: entity == null ? "新增组织机构" : "编辑组织机构",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 400,
            height: 160,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 50,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "Id",
                            value: entity == null ? null : entity.id
                        },
                        {
                            id: "editName",
                            fieldLabel: "名称",
                            allowBlank: false,
                            blankText: "没有输入名称",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            name: "Name",
                            value: entity == null ? null : entity.text
                        },
                        {
                            id: "editParentOrg",
                            xtype: "psi_parent_org_editor",
                            parentItem: this,
                            fieldLabel: "上级组织"
                        },
                        {
                            id: "editParentOrgId",
                            xtype: "hidden",
                            name: "ParentId",
                            value: entity == null ? null : entity.parentId
                        },
                        {
                            id: "editOrgCode",
                            fieldLabel: "编码",
                            allowBlank: false,
                            blankText: "没有输入编码",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            name: "OrgCode",
                            value: entity == null ? null : entity.orgCode
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: this.onOK,
                            scope: this
                        },
                        {
                            text: "取消", handler: function () {
                                var me = this;
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: this
                        }
                    ]
                }
            ]
            
        });

        this.wnd = wnd;
    },

    show: function (entity) {
        this.createUI(entity);

        if (entity) {
            this.wnd.on("show", function () { this.onEditFormShow(entity); }, this);
        }

        this.wnd.show();
    },

    onEditFormShow: function (entity) {
        var form = this.wnd;
        form.getEl().mask("数据加载中...");
        Ext.Ajax.request({
            url: "/User/GetOrg",
            method: "POST",
            params: { id: entity.id },
            callback: function (options, success, response) {
                form.getEl().unmask();
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    var editParentOrg = Ext.getCmp("editParentOrg");
                    editParentOrg.setValue(data.parentOrgName);
                } else {
                    PSI.MsgBox.showInfo("网络错误", function () {
                        window.location.reload();
                    });
                }
            }
        });
    },

    setParentOrg: function (data) {
        var editParentOrg = Ext.getCmp("editParentOrg");
        editParentOrg.setValue(data.fullName);
        var editParentOrgId = Ext.getCmp("editParentOrgId");
        editParentOrgId.setValue(data.id);
    },

    // private
    onOK: function () {
        var f = Ext.getCmp("editForm");
        var el = f.getEl();
        el.mask("数据保存中...");
        f.submit({
            url: "/User/EditOrg",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo("数据保存成功", function () {
                    window.location.reload();
                });
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg, function () {
                    Ext.getCmp("editName").focus();
                });
            }
        });
    }
});